package com.cqfae.deploycentral.app.web.controller;

/**
 * Created by YANGHENG940 on 2017/7/12.
 */

import com.cqfae.deploycentral.app.utils.GsonUtils;
import com.cqfae.deploycentral.app.base.BaseConstants;
import com.cqfae.deploycentral.app.services.CommonControllerService;
import com.cqfae.deploycentral.app.services.ReportInfoService;
import com.cqfae.deploycentral.app.utils.NullUtil;
import com.cqfae.deploycentral.app.web.request.ReportInfoRequest;
import com.cqfae.deploycentral.app.web.request.ReportTagRequest;
import com.cqfae.deploycentral.app.web.response.WebResponse;
import com.cqfae.deploycentral.app.web.vo.ReportInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>测试报告controller</p>
 *
 * @author yangheng@cmhk.com
 * @since 1.0.0-snapshot
 */
@RestController
@RequestMapping("/deploy-central/report")
public class ReportInfoController implements CommonControllerService<Object> {

    private static Logger logger = LoggerFactory.getLogger(ReportInfoController.class);

    @Autowired
    private ReportInfoService reportInfoService;

    @RequestMapping(value = "/info")
    public WebResponse pageInfo(ReportInfoRequest request) {
        String requestJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== pageInfo ReportInfoRequest :==== {}", requestJson);
        return getWebResponse(reportInfoService.page(request));
    }

    @RequestMapping(value = "/infoOne")
    public WebResponse infoOne(ReportInfoRequest request) {
        String requestJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== infoOne ReportInfoRequest :==== {}", requestJson);
        Assert.isTrue(NullUtil.isNotEmpty(request.getReportCode()), "测试报告编号不能为空");
        return getWebResponse(reportInfoService.getOne(request));
    }

    @RequestMapping(value = "/selected")
    public WebResponse selectedInfo(ReportInfoRequest request) {
        String requestJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== selectedInfo ReportInfoRequest :==== {}", requestJson);
        return getWebResponse(reportInfoService.getSelectedList(request));
    }

    @RequestMapping(value = "/report-selected")
    public WebResponse getReportSelected(ReportInfoRequest request) {
        String requestJson = GsonUtils.getInstance().toJson(request);
        logger.info("==== getReportSelected ReportInfoRequest :==== {}", requestJson);
        return getWebResponse(reportInfoService.getReportSelected(request));
    }

    @RequestMapping(value = "/add")
    public WebResponse addInfo(@RequestBody ReportInfoVo reportInfoVo) {
        String requestJson = GsonUtils.getInstance().toJson(reportInfoVo);
        logger.info("==== addInfo reportInfoVo :==== {}", requestJson);
        return getWebResponse(reportInfoService.save(reportInfoVo));
    }

    /**
     * 提交至待发布
     *
     * @param reportInfoVo
     * @return
     */
    @RequestMapping(value = "/commit-public")
    public WebResponse commitPublic(@RequestBody ReportInfoVo reportInfoVo) {
        String reportInfoVoJson = GsonUtils.getInstance().toJson(reportInfoVo);
        logger.info("==== commitPublic reportInfoVo :==== {}", reportInfoVoJson);
        Assert.isTrue(NullUtil.isNotEmpty(reportInfoVo.getReportCode()), "reportCode不能为空");
        return getWebResponse(reportInfoService.commitPublic(reportInfoVo));
    }

    @RequestMapping(value = "/deleteOneTag")
    public WebResponse deleteOneTag(@RequestBody ReportTagRequest request) {
        Assert.isTrue(NullUtil.isNotEmpty(request) && request.getId() > 0, "非法测试报告tagId");
        logger.info("==== ReportInfoRequest deleteOneTag:==== {}", request.getId());
        return getWebResponse(reportInfoService.deleteOneTag(request.getId()));
    }


    /**
     * 更新测试报告
     *
     * @param reportInfoVo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    public WebResponse edit(@RequestBody ReportInfoVo reportInfoVo) {
        String requestJson = GsonUtils.getInstance().toJson(reportInfoVo);
        logger.info("==== edit ReportInfoRequest :==== {}", requestJson);
        Assert.isTrue(NullUtil.isNotEmpty(reportInfoVo) && reportInfoVo.getId() > 0, "对象为空或测试报告id不正确");
        Integer i = reportInfoService.edit(reportInfoVo);
        return getWebResponse(i);
    }

    /**
     * 获取testLink数据
     *
     * @return
     */
    @RequestMapping(value = "/get-testlink")
    public WebResponse getTestLink(String reportCode) {
        logger.info("==== ReportInfoController.getTestLink: {}==== ", reportCode);
        return getWebResponse(reportInfoService.getTestLink(reportCode));
    }

    /**
     * 获取testLink数据
     *
     * @return
     */
    @RequestMapping(value = "/get-jira-version")
    public WebResponse getJiraVersion(String reportCode) {
        logger.info("==== ReportInfoController.getJiraVersion:{}==== ", reportCode);
        return getWebResponse(reportInfoService.getJiraVersion(reportCode));
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/file/upload", method = RequestMethod.POST)
    public WebResponse fileUpload(@RequestParam("file") MultipartFile file) {
        Assert.isTrue((file.getSize() > 0 && file.getSize() < BaseConstants.FILE_LIMIT_BYTES), "文件上传大小必须在1kb到10M之间");
        return getWebResponse(reportInfoService.fileUpload(file));
    }

    /**
     * 文件删除
     *
     * @param fileId
     * @return
     */
    @RequestMapping(value = "/file/delete")
    public WebResponse fileDelete(String fileId) {
        logger.info("==== ReportInfoController.fileDelete:{}==== ", fileId);
        return getWebResponse(reportInfoService.fileDelete(fileId));
    }

    /**
     * 文件下载
     *
     * @param fileId
     * @param response
     */
    @RequestMapping(value = "/file/download")
    public void exportFile(long fileId, HttpServletResponse response) {
        logger.info("==== ReportInfoController.exportFile fileId :{}==== ", fileId);
        reportInfoService.downloadFile(fileId, response);
    }


    /**
     * 获取测试计划
     *
     * @param reportInfoRequest
     * @return
     */
    @RequestMapping(value = "/get-testplan", method = RequestMethod.POST)
    public WebResponse getTestPlan(ReportInfoRequest reportInfoRequest) {
        logger.info("==== ReportInfoController.getTestPlan: {}==== ", reportInfoRequest);
        return getWebResponse(reportInfoService.getTestPlan(reportInfoRequest));
    }

    /**
     * 获取测试计划
     *
     * @param reportInfoRequest
     * @return
     */
    @RequestMapping(value = "/update-uat-flag", method = RequestMethod.POST)
    public WebResponse updateUatFlag(@RequestBody ReportInfoRequest reportInfoRequest) {
        logger.info("==== ReportInfoController.updateUatFlag: {}==== ", reportInfoRequest);
        return getWebResponse(reportInfoService.updateUatFlag(reportInfoRequest));
    }

    /**
     * 生成pdf
     *
     * @param reportCode
     * @return
     */
    @RequestMapping(value = "/get-pdf")
    public WebResponse getPdf(String reportCode) {
        logger.info("==== ReportInfoController.getPdf: {}==== ", reportCode);
        return getWebResponse(reportInfoService.getPdf(reportCode));
    }


    @Override
    public WebResponse getWebResponse(Object obj) {
        WebResponse webResponse = new WebResponse();
        if (NullUtil.isNotEmpty(obj)) {
            webResponse.setData(obj);
        }
        return webResponse;
    }
}
